// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Moving Block Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 61 : TYPE_MOVINGBLOCK

// Function declarations
reserve function MovingBlock_DebugDraw
reserve function MovingBlock_DebugSpawn

// Static Values

// Tables

function MovingBlock_DebugDraw
	DrawSprite(0)
end function


function MovingBlock_DebugSpawn
	CreateTempObject(TypeName[Moving Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].priority = PRIORITY_XBOUNDS
	object[tempObjectPos].state = 1
end function


event ObjectMain
	if object.state < 3
		temp4 = stage.waterLevel
		temp4 <<= 16
		temp3 = object.ypos
		if temp3 != temp4
			if temp3 < temp4
				temp3 += 0x10000
				if temp3 < temp4
					temp3 += 0x10000
				end if
			else
				temp3 -= 0x10000
				if temp3 > temp4
					temp3 -= 0x10000
				end if
			end if
		end if
		object.yvel = temp3
		object.yvel -= object.ypos
		if object.yvel > 0
			ObjectTileCollision(CSIDE_FLOOR, 0, 16, 0)
		else
			ObjectTileCollision(CSIDE_ROOF, 0, -16, 0)
		end if
	end if
	
	
	switch object.state
	case 0
		if object[+2].value0 == 1
			object.state++
		end if
		break
	case 1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.state++
				object[currentPlayer].ypos += object.yvel
				if stage.debugMode == 0
					if currentPlayer == 0
						achievement.trigger = 5
						//CallNativeFunction2(SetAchievement, 5, 100)
					end if
				end if
			end if
		next
		object.ypos += object.yvel
		break
	case 2
		temp0 = object.xpos
		temp0 &= -0x10000
		object.xpos += 0x10000
		ObjectTileCollision(CSIDE_LWALL, 16, 0, 0)
		if checkResult == 1
			object.state++
		end if
		object.value0 = object.xpos
		object.value0 &= -0x10000
		object.value0 -= temp0
		temp1 = object.xpos
		object.xpos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos += object.value0
				object[currentPlayer].ypos += object.yvel
			end if
		next
		object.xpos = temp1
		object.ypos += object.yvel
		break
	case 3
		temp0 = object.ypos
		temp0 &= -0x10000
		object.ypos += object.yvel
		object.yvel += 0x3800
		ObjectTileCollision(CSIDE_FLOOR, 0, 8, 0)
		if checkResult == 1
			object.state++
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		object.value0 = object.ypos
		object.value0 &= -0x10000
		object.value0 -= temp0
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ypos += object.value0
			end if
			if object[currentPlayer].gravity == GRAVITY_GROUND
				if checkResult == 4
					CallFunction(PlayerObject_Kill)
				end if
			end if
		next
		object.ypos = temp1
		break
	case 4
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	end switch
end event


event ObjectDraw
	if object.state > 0
		DrawSprite(0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 1, 1)
	foreach (TypeName[Moving Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
	next
	SetTableValue(TypeName[Moving Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MovingBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MovingBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
